package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Report;
import com.itheima.service.MemberService;
import com.itheima.service.ReportService;
import com.itheima.utils.POIUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * @author M.F.S
 */
@RestController
@RequestMapping("report")
public class ReportController {

    @Reference(version = "1.0.0")
    private ReportService reportService;

    @PostMapping("fileUpload")
    public Result fileUpload(@RequestParam("fileName") MultipartFile fileName) {
        try {
            //调用工具类读取文件
            List<String[]> list = POIUtils.readExcel(fileName);
            //调用业务层进行数据存储
            reportService.fileUpload(list);
            //返回上传结果
            return new Result(true, "文件上传成功!");
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, "文件上传失败!");
        }
    }

    @PostMapping("add/{fileNumber}")
    public Result add(@PathVariable String fileNumber) {
        //调用业务层进行数据存储
        try {
            reportService.add(fileNumber);
            return new Result(true, "文件上传成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        //返回上传结果
        return new Result(false, "文件上传失败!");
    }


    /**
     * 分页查询方法
     *
     * @param queryPageBean 分页查询会员实体类
     * @return 分页查询结果，总条数与当前页数据
     */
    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = reportService.findPage(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return pageResult;
    }

    /**
     * 删除
     *
     * @param id 要删除的档案号
     * @return 编辑操作的结果
     */
    @GetMapping("delete/{id}")
    public Result delete(@PathVariable String id) {
        //调用业务层删除方法
        try {
            reportService.delete(id);
            //返回操作结果
            return new Result(true, "删除体检报告成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除体检报告失败!");
        }
    }

    @RequestMapping("/getMemberReport")
    public Result getMemberReport(){
        HashMap<String, Object> map = new HashMap<>();
        ArrayList<String> months = new ArrayList<>();
        //获得日历对象,模拟时间就是当前时间
        Calendar calendar = Calendar.getInstance();
        //计算过去一年的12个月
        calendar.add(Calendar.MONTH,-12);
        for (int i = 0; i < 12; i++) {
            //获取当前时间往后推一个月日期
            calendar.add(Calendar.MONTH,1);
            Date date = calendar.getTime();
            months.add(new SimpleDateFormat("yyyy.MM").format(date));
        }
        map.put("months",months);

        //根据月份查询会员数量
        List<Integer> memberCount = reportService.findMemberCountByMonths(months);
        map.put("memberCount",memberCount);
        return new Result(true,MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);
    }


    @PostMapping("getSetMealReport")
    public Result getSetMealReport(){
        return reportService.getSetMealReport();
    }
}
